﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using System.Configuration;
using System.Data.SqlClient;

public partial class Admin_Admin_BackUpDB : Admin, IRequiresSessionState
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckSuperAdmin())
        {
            base.WriteLog("View", Language.GetAdminMsg("Log/Access", "0"), "BackUpDB");
            base.WriteRightErrMsg();
        }
    }
    protected void Btn_BackUp_Click(object sender, EventArgs e)
    {
        if (this.BackUpName.Text != "" && this.BackUpName.Text != null)
        {
            FileIO fileIO = new FileIO();
            string str = this.BackUpName.Text + ".bak";
            string text = this.BackUpDir.Text;
            string connectionString = ConfigurationManager.ConnectionStrings["HowaveConnectionString"].ConnectionString;
            string dbName = this.GetDbName(connectionString);
            string cmdText = string.Concat(new string[]
			{
				"backup database [",
				dbName,
				"] to disk = '",
				fileIO.GetTruePath("\\" + text + "\\" + str),
				"'"
			});
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            if (fileIO.FileExists(fileIO.GetTruePath("\\" + text + "\\" + str)))
            {
                this.lblbackup.Text = Language.GetAdminMsg("DataBaseBak/FileExists");
            }
            else
            {
                try
                {
                    fileIO.CreateFile(fileIO.GetTruePath("\\" + text + "\\" + str));
                }
                catch (Exception ex)
                {
                    this.lblbackup.Text = ex.Message.ToString();
                    return;
                }
                try
                {
                    sqlConnection.Open();
                    SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
                    sqlCommand.ExecuteNonQuery();
                    this.lblbackup.Text = "<font color=red>" + Language.Get("Common/Success") + "</font>";
                }
                catch (Exception ex2)
                {
                    this.lblbackup.Text = ex2.Message.ToString();
                }
            }
        }
        else
        {
            this.lblbackup.Text = Language.GetAdminMsg("DataBaseBak/FileNameEmpty");
        }
    }
    protected void Btn_RestoreDB_Click(object sender, EventArgs e)
    {
        string text = this.RestoreDBName.Text;
        if (text != "" && text != null)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["HowaveConnectionString"].ConnectionString;
            string dbName = this.GetDbName(connectionString);
            string text2 = "Alter Database [" + dbName + "] Set Offline with Rollback immediate;";
            text2 = text2 + "restore database [" + dbName + "] from disk = '";
            text2 = text2 + base.Server.MapPath(text).ToString() + "'";
            text2 = text2 + "Alter Database [" + dbName + "] Set OnLine With rollback Immediate;";
            try
            {
                string connectionString2 = string.Concat(new string[]
				{
					"Server=",
					this.R_DBSource.Text,
					";database=master;uid=",
					this.R_DBUser.Text,
					";pwd=",
					this.R_DBPassword.Text,
					";"
				});
                SqlConnection sqlConnection = new SqlConnection(connectionString2);
                sqlConnection.Open();
                SqlCommand sqlCommand = new SqlCommand(text2, sqlConnection);
                sqlCommand.ExecuteNonQuery();
                this.lblRestore.Text = Language.Get("Common/Success");
            }
            catch (Exception ex)
            {
                this.lblRestore.Text = Language.Get("Common/Fail", ex.Message.ToString());
            }
        }
        else
        {
            this.lblbackup.Text = Language.GetAdminMsg("DataBaseBak/FileNameEmpty");
        }
    }
    protected string GetDbName(string Connstring)
    {
        string[] array = Connstring.Split(new char[]
		{
			';'
		});
        string[] array2 = array[1].Split(new char[]
		{
			'='
		});
        return array2[1];
    }
}
